﻿using System;
using System.Collections.Generic;
using FormicArch.Core;
using Plugins.FormicArch.Core;

namespace GameFramework
{
    public partial class GameFacade
    {
     
        private Dictionary<PubliceEventType, object> EventHandlers { get; set; } =
            new Dictionary<PubliceEventType, object>();
        
        protected void UnregisterAll()
        {
            if (EventHandlers == null)
            {
                return;
            }

            foreach (var eventHandler in EventHandlers)
            {
                UnregisterByObj(eventHandler.Key, eventHandler.Value);
            }
            EventHandlers.Clear();
            EventHandlers = null;
        }

        // 注册事件（支持 1 到 5 个参数）
        public void RegisterByObj(PubliceEventType publiceEventType, object handler)
        {
            EventHandlers.TryAdd(publiceEventType, handler);
            FAEventManager.Register(publiceEventType, handler);
           
        }

        public void Register(PubliceEventType publiceEventType, Action handler) =>
            RegisterByObj(publiceEventType, handler);

        public void Register<T>(PubliceEventType publiceEventType, Action<T> handler) =>
            RegisterByObj(publiceEventType, handler);

        public void Register<T1, T2>(PubliceEventType publiceEventType, Action<T1, T2> handler) =>
            RegisterByObj(publiceEventType, handler);

        public void Register<T1, T2, T3>(PubliceEventType publiceEventType, Action<T1, T2, T3> handler) =>
            RegisterByObj(publiceEventType, handler);

        public void Register<T1, T2, T3, T4>(PubliceEventType publiceEventType, Action<T1, T2, T3, T4> handler) =>
            RegisterByObj(publiceEventType, handler);

        public void Register<T1, T2, T3, T4, T5>(PubliceEventType publiceEventType,
            Action<T1, T2, T3, T4, T5> handler) =>
            RegisterByObj(publiceEventType, handler);

        // 取消注册事件（支持 1 到 5 个参数）
        private void UnregisterByObj(PubliceEventType publiceEventType, object handler) =>
            FAEventManager.Unregister(publiceEventType, handler);

        public void Unregister(PubliceEventType publiceEventType, Action handler) =>
            UnregisterByObj(publiceEventType, handler);

        public void Unregister<T>(PubliceEventType publiceEventType, Action<T> handler) =>
            UnregisterByObj(publiceEventType, handler);

        public void Unregister<T1, T2>(PubliceEventType publiceEventType, Action<T1, T2> handler) =>
            UnregisterByObj(publiceEventType, handler);

        public void Unregister<T1, T2, T3>(PubliceEventType publiceEventType, Action<T1, T2, T3> handler) =>
            UnregisterByObj(publiceEventType, handler);

        public void Unregister<T1, T2, T3, T4>(PubliceEventType publiceEventType, Action<T1, T2, T3, T4> handler) =>
            UnregisterByObj(publiceEventType, handler);

        public void Unregister<T1, T2, T3, T4, T5>(PubliceEventType publiceEventType,
            Action<T1, T2, T3, T4, T5> handler) =>
            UnregisterByObj(publiceEventType, handler);

        // 触发事件（支持 1 到 5 个参数）
        public void Trigger(PubliceEventType publiceEventType) =>
            FAEventManager.Trigger(publiceEventType);

        public void Trigger<T>(PubliceEventType publiceEventType, T arg) =>
            FAEventManager.Trigger(publiceEventType, arg);

        public void Trigger<T1, T2>(PubliceEventType publiceEventType, T1 arg1, T2 arg2) =>
            FAEventManager.Trigger(publiceEventType, arg1, arg2);

        public void Trigger<T1, T2, T3>(PubliceEventType publiceEventType, T1 arg1, T2 arg2, T3 arg3) =>
            FAEventManager.Trigger(publiceEventType, arg1, arg2, arg3);

        public void Trigger<T1, T2, T3, T4>(PubliceEventType publiceEventType, T1 arg1, T2 arg2, T3 arg3, T4 arg4) =>
            FAEventManager.Trigger(publiceEventType, arg1, arg2, arg3, arg4);


        public void Trigger<T1, T2, T3, T4, T5>(PubliceEventType publiceEventType, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
            T5 arg5) =>
            FAEventManager.Trigger(publiceEventType, arg1, arg2, arg3, arg4, arg5);
    }
}